//
//  MAssessViewController.m
//  Gunz
//
//  Created by Joggy on 16/8/16.
//  Copyright © 2016年 NUPT. All rights reserved.
//

#import "MAssessViewController.h"
#import "BarButtonView.h"
#import "QYNotice.h"
#import "GZConst.h"

@interface MAssessViewController () {
    
    NSMutableArray *buttonArray;
    UITextView *assessView;
    int assessRange;
    BarButtonView *barButton;
    
}

@end

@implementation MAssessViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self prepareForNav];
    [self prepareForLayout];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(endEdit)];
    [self.view addGestureRecognizer:tap];
}

- (void)prepareForNav {
    [super prepareForNav];
    self.title = @"评价";
    //导航左按键
    UIImage *image = [UIImage imageNamed:@"all_back"];
    UIButton *leftBarButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftBarButton.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    leftBarButton.layer.position = CGPointMake(22, 22);
    [leftBarButton setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [leftBarButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBarButton];
}

- (void)prepareForLayout {
    [super prepareForLayout];
    
    UILabel *assessLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 40)];
    assessLabel.center = CGPointMake(screenWidth/2, 66);
    assessLabel.text = @"评价等级:";
    assessLabel.textAlignment = NSTextAlignmentCenter;
    assessLabel.alpha = 0.5;
    [self.view addSubview:assessLabel];
    
    assessRange = 0;
    buttonArray = [[NSMutableArray alloc] initWithCapacity:5];
    for(int i = 0; i < 5; i++) {
        UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
        but.frame = CGRectMake(0, 0, 44, 44);
        but.center = CGPointMake((screenWidth/8)*(2 + i), assessLabel.frame.origin.y + 66);
        [but setImage:[UIImage imageNamed:@"mine_assess_normal"] forState:UIControlStateNormal];
        [but setImage:[UIImage imageNamed:@"mine_assess_selected"] forState:UIControlStateSelected];
        [but addTarget:self action:@selector(assessRank:) forControlEvents:UIControlEventTouchUpInside];
        but.tag = i;
        [self.view addSubview:but];
        [buttonArray addObject:but];
    }
    
    assessView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, screenWidth - 50, (screenWidth - 40)*2/3)];
    assessView.center = CGPointMake(screenWidth/2, assessLabel.frame.origin.y + assessView.frame.size.height/2 + 115);
    assessView.layer.cornerRadius = 6;
    assessView.layer.borderColor = TEXT_BORDER_COLOR.CGColor;
    assessView.layer.borderWidth = 1;
    [self.view addSubview:assessView];
    
//    BarButtonView *barButton = [[BarButtonView alloc] initWithFrame:CGRectMake(0, [GZConst getScreenHeight] - 64 - 56, [GZConst getScreenWidth], BBViewHeight) name:@"完成点评"];
    barButton = [[BarButtonView alloc] initWithTitle:@"完成点评"];
    [barButton.but addTarget:self action:@selector(submitComment) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:barButton];
    
}

- (void)submitComment {
    
    [QYNotice showSuccess:@"提交成功" toView:self.view];
    [GZConst delay:3 with:^{
        [self back];
    }];
    
}

- (void)assessRank:(UIButton*)button {
    assessRange = (int)button.tag;
    for(int i = 0; i <= button.tag; i++) {
        UIButton *but = buttonArray[i];
        but.selected = true;
    }
    for(int j = 4; j > button.tag; j--) {
        UIButton *but = buttonArray[j];
        but.selected = false;
    }
}

- (void)back {
    [self.navigationController popViewControllerAnimated:true];
}

- (void)endEdit {
    [self.view endEditing:true];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
